<?php
namespace app\common\model;
use app\admin\model\ucenter\Member;
use \think\facade\Log;
use think\Model;
use think\Facade\Db;

class Wechat extends Model
{
	protected $name = 'wx_config';
	
	protected $pk = 'id';
	protected $autoWriteTimestamp = true;
	protected $type       = [
        'createtime' => 'timestamp:Y-m-d H:i:s',
        'updatetime' => 'timestamp:Y-m-d H:i:s',
    ];
    protected $createTime='createtime';
    protected $updateTime='updatetime';

    public function member()
    {
        return $this->hasMany(Member::class,'wx_appid');
    }
    /**
     * 获取列表
     * @return array 列表
     */
    public static function loadList($where=[],$limit=20,$field='*',$orderby=''){	
    	$result = array();
    	
		try{
			empty($orderby) && $orderby = 'id desc';
			$lists =  self::where($where)->field($field)->order($orderby)->paginate(['list_rows'=> $limit,'page'=>1]);
			$list = $lists->toArray();
			$page = $lists->render();
			$result = ['list' => $list['data'], 'page' => $page, 'count' => $list['total']];
		}catch(\Exception $e){
			throw new \Exception($e->getMessage());
		}
		
		return $result;
    }
    /**
     * 获取微信菜单
     * @return array 列表
     */
    public static function loadWxmenu($where=[],$limit=20,$field='*',$orderby=''){	
    	$result = array();
    	
		try{
			empty($orderby) && $orderby = 'id desc';
			$lists =  DB::name('wx_menu')->where($where)->field($field)->order($orderby)->paginate($limit)->each(function($item, $key){
				    return $item;
				});
			$list = $lists->toArray();
			$page = $lists->render();
			$result = ['list' => $list['data'], 'page' => $page, 'count' => $list['total']];
		}catch(\Exception $e){
			throw new \Exception($e->getMessage());
		}
		
		return $result;
    }

}
